package zct.proj.equipModel;

import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Point;
import zct.proj.pojo.EquipmentInfo;
import zct.proj.util.Const;

public class XHJModel extends BaseModel {

	// 信号机灯圈半径
	private int radius;
	
	/**
	 * 单灯矮柱
	 */
	private XHJInfo ddaz;
	private XHJInfo rddaz;
	private XHJInfo ddaz90;
	private XHJInfo ddaz270;
	
	/**
	 * 单灯高柱
	 */
	private XHJInfo ddgz;
	private XHJInfo rddgz;
	private XHJInfo ddgz90;
	private XHJInfo ddgz270;	
	/**
	 * 双灯矮柱
	 */
	private XHJInfo sdaz;
	private XHJInfo rsdaz;
	private XHJInfo sdaz90;
	private XHJInfo sdaz270;
	
	/**
	 * 双灯高柱
	 */
	private XHJInfo sdgz;
	private XHJInfo rsdgz;
	private XHJInfo sdgz90;
	private XHJInfo sdgz270;
	
	public XHJModel(float scale, int cellWidth, int cellHeight) {
		super(scale, cellWidth, cellHeight);
	}

	protected void init() {
		super.init();
		radius = (int) (Const.XHJ_RING_RATE * ySpacing);
	}
	
	/**
	 * 调车矮柱 |O  出发单灯矮柱 |O
	 * @param fontSize
	 * @param fontName
	 */
	private void initDDAZ(int fontSize, String fontName) {
		// 正向
		Path path = new Path();
		path.moveTo(xSpacing, ySpacing);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 确定灯心，画圆
		Point pc = new Point(xSpacing + radius, (int) (1.5f * ySpacing));
		path.addCircle(pc.x, pc.y, radius, Direction.CW);
		// 生成对象
		ddaz = new XHJInfo(path, pc);
		// 设置文本样式
		Point pt = new Point(xSpacing, ySpacing - Const.TEXT_DRAW_DISTANCE);
		ddaz.setFont(pt, Align.LEFT);

		// 反向
		path = new Path();
		path.moveTo(xSpacing, 0);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 确定灯心，画圆
		pc = new Point(xSpacing - radius, (int) (0.5f * ySpacing));
		path.addCircle(pc.x, pc.y, radius, Direction.CW);
		// 生成对象
		rddaz = new XHJInfo(path, pc);
		// 设置文本样式
		pt = new Point(0, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		rddaz.setFont(pt, Align.LEFT);
	}

	/**
	 * 调车高柱 |-O  出发单灯高柱 |-O
	 * @param fontSize
	 * @param fontName
	 */
	private void initDDGZ(int fontSize, String fontName) {
		int hStub = (int) (Const.XHJ_SHORT_HLINE_RATE * xSpacing);
		// 正向
		Path path = new Path();
		path.moveTo(xSpacing, ySpacing);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 画短柱
		path.moveTo(xSpacing, 1.5f * ySpacing);
		path.rLineTo(hStub, 0);
		// 确定灯心，画圆
		Point pc = new Point(xSpacing + radius + hStub, (int) (1.5f * ySpacing));
		path.addCircle(pc.x, pc.y, radius, Direction.CW);
		// 生成对象
		ddgz = new XHJInfo(path, pc);
		// 设置文本样式
		Point pt = new Point(xSpacing, ySpacing - Const.TEXT_DRAW_DISTANCE);
		ddgz.setFont(pt, Align.LEFT);

		// 反向
		path = new Path();
		path.moveTo(xSpacing, 0);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 画短柱
		path.moveTo(xSpacing, 0.5f * ySpacing);
		path.rLineTo(-hStub, 0);
		// 确定灯心，画圆
		pc = new Point(xSpacing - radius - hStub, (int) (0.5f * ySpacing));
		path.addCircle(pc.x, pc.y, radius, Direction.CW);
		// 生成对象
		rddgz = new XHJInfo(path, pc);
		// 设置文本样式
		pt = new Point(0, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		rddgz.setFont(pt, Align.LEFT);
	}
	
	/**
	 * 出发双灯矮柱 |OO
	 * @param fontSize
	 * @param fontName
	 */
	private void initSDAZ(int fontSize, String fontName) {
		// 正向
		Path path = new Path();
		path.moveTo(xSpacing, ySpacing * 2);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 确定灯心，画圆
		Point pc1 = new Point(xSpacing + radius, (int) (2.5f * ySpacing));
		Point pc2 = new Point(xSpacing + radius * 3, (int) (2.5f * ySpacing));
		path.addCircle(pc1.x, pc1.y, radius, Direction.CW);
		path.addCircle(pc2.x, pc2.y, radius, Direction.CW);
		// 生成对象
		sdaz = new XHJInfo(path, pc1, pc2);
		// 设置文本样式
		Point pt = new Point(xSpacing, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		sdaz.setFont(pt, Align.LEFT);

		// 反向
		path = new Path();
		path.moveTo(xSpacing * 2, 0);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 确定灯心，画圆
		pc1 = new Point(xSpacing * 2 - radius * 3, (int) (0.5f * ySpacing));
		pc2 = new Point(xSpacing * 2 - radius, (int) (0.5f * ySpacing));
		path.addCircle(pc1.x, pc1.y, radius, Direction.CW);
		path.addCircle(pc2.x, pc2.y, radius, Direction.CW);
		// 生成对象  反向时交换等位置
		rsdaz = new XHJInfo(path, pc2, pc1);
		// 设置文本样式
		pt = new Point(0, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		rsdaz.setFont(pt, Align.LEFT);
	}

	/**
	 * 出发双灯高柱 |-OO  进站 |-OO  区间 |-OO
	 * @param fontSize
	 * @param fontName
	 */
	private void initSDGZ(int fontSize, String fontName) {
		int hStub = (int) (Const.XHJ_SHORT_HLINE_RATE * xSpacing);
		// 正向
		Path path = new Path();
		path.moveTo(xSpacing, ySpacing * 2);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 画短柱
		path.moveTo(xSpacing, 2.5f * ySpacing);
		path.rLineTo(hStub, 0);
		// 确定灯心，画圆
		Point pc1 = new Point(xSpacing + radius + hStub, (int) (2.5f * ySpacing));
		Point pc2 = new Point(xSpacing + radius * 3 + hStub, (int) (2.5f * ySpacing));
		path.addCircle(pc1.x, pc1.y, radius, Direction.CW);
		path.addCircle(pc2.x, pc2.y, radius, Direction.CW);
		// 生成对象
		sdgz = new XHJInfo(path, pc1, pc2);
		// 设置文本样式
		Point pt = new Point(xSpacing, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		sdgz.setFont(pt, Align.LEFT);

		// 反向
		path = new Path();
		path.moveTo(xSpacing * 2, 0);
		// 画立柱
		path.rLineTo(0, ySpacing);
		// 画短柱
		path.moveTo(xSpacing * 2 - hStub, 0.5f * ySpacing);
		path.rLineTo(hStub, 0);
		// 确定灯心，画圆
		pc1 = new Point(xSpacing * 2 - radius * 3 - hStub, (int) (0.5f * ySpacing));
		pc2 = new Point(xSpacing * 2 - radius - hStub, (int) (0.5f * ySpacing));
		path.addCircle(pc1.x, pc1.y, radius, Direction.CW);
		path.addCircle(pc2.x, pc2.y, radius, Direction.CW);
		// 生成对象
		rsdgz = new XHJInfo(path, pc2, pc1);
		// 设置文本样式
		pt = new Point(0, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		rsdgz.setFont(pt, Align.LEFT);
		
		// 顺时针90度
		path = new Path();
		path.moveTo(xSpacing * 2, ySpacing * 2);
		// 画立柱
		path.rLineTo(xSpacing, 0);
		// 画短柱
		path.moveTo(2.5f * xSpacing, ySpacing * 2 - hStub);
		path.rLineTo(0, hStub);
		// 确定灯心，画圆
		pc1 = new Point((int) (2.5f * xSpacing), ySpacing * 2 - radius * 3 - hStub);
		pc2 = new Point((int) (2.5f * xSpacing), ySpacing * 2 - radius - hStub);
		path.addCircle(pc1.x, pc1.y, radius, Direction.CW);
		path.addCircle(pc2.x, pc2.y, radius, Direction.CW);
		// 生成对象
		sdgz90 = new XHJInfo(path, pc2, pc1);
		// 设置文本样式
		pt = new Point(xSpacing * 3, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
		sdgz90.setFont(pt, Align.LEFT);
		
//		sdgz270 = sdgz90;
		
//		// 顺时针270度
//		path = new Path();
//		path.moveTo(xSpacing * 2, 0);
//		// 画立柱
//		path.rLineTo(0, ySpacing);
//		// 画短柱
//		path.moveTo(xSpacing * 2 - hStub, 0.5f * ySpacing);
//		path.rLineTo(hStub, 0);
//		// 确定灯心，画圆
//		pc1 = new Point(xSpacing * 2 - radius * 3 - hStub, (int) (0.5f * ySpacing));
//		pc2 = new Point(xSpacing * 2 - radius - hStub, (int) (0.5f * ySpacing));
//		path.addCircle(pc1.x, pc1.y, radius, Direction.CW);
//		path.addCircle(pc2.x, pc2.y, radius, Direction.CW);
//		// 生成对象
//		rsdgz = new XHJInfo(path, pc2, pc1);
//		// 设置文本样式
//		pt = new Point(0, ySpacing * 2 - Const.TEXT_DRAW_DISTANCE);
//		rsdgz.setFont(pt, Align.LEFT);
	}

	public XHJInfo getXHJInfo(EquipmentInfo sb) {
		
		if (ddaz == null) {
			initXHJInfo(sb.getFontsize(), sb.getFont());
		}
		
		if (sb.getType() == Const.TYPE_XHJ_DCAZ
				|| sb.getType() == Const.TYPE_XHJ_CFDDAZ) {
			// 调车矮柱 |O  出发单灯矮柱 |O
			switch (sb.getDir()) {
			case Const.DIR_NORMAL:
				return ddaz;
			case Const.DIR_ROTATE:
				return rddaz;
			case Const.DIR_HCOPY:
				return ddaz90;
			case Const.DIR_VCOPY:
				return ddaz270;
			}
		} else if (sb.getType() == Const.TYPE_XHJ_DCGZ
				|| sb.getType() == Const.TYPE_XHJ_CFDDGZ) {
			// 调车高柱 |-O  出发单灯高柱 |-O
			switch (sb.getDir()) {
			case Const.DIR_NORMAL:
				return ddgz;
			case Const.DIR_ROTATE:
				return rddgz;
			case Const.DIR_HCOPY:
				return ddgz90;
			case Const.DIR_VCOPY:
				return ddgz270;
			}
		} else if (sb.getType() == Const.TYPE_XHJ_CFSDAZ) {
			// 出发双灯矮柱 |OO
			switch (sb.getDir()) {
			case Const.DIR_NORMAL:
				return sdaz;
			case Const.DIR_ROTATE:
				return rsdaz;
			case Const.DIR_HCOPY:
				return sdaz90;
			case Const.DIR_VCOPY:
				return sdaz270;
			}
		} else if (sb.getType() == Const.TYPE_XHJ_CFSDGZ
				 || sb.getType() == Const.TYPE_XHJ_JZ
				 || sb.getType() == Const.TYPE_XHJ_QJ) {
			// 出发双灯高柱 |-OO  进站 |-OO  区间 |-OO
			switch (sb.getDir()) {
			case Const.DIR_NORMAL:
				return sdgz;
			case Const.DIR_ROTATE:
				return rsdgz;
			case Const.DIR_HCOPY:
				return sdgz90;
			case Const.DIR_VCOPY:
				return sdgz270;
			}
		}
		return null;
	}
	
	/**
	 * 初始化，生成所有信号机绘图路径
	 * @param fontSize
	 * @param fontName
	 */
	public void initXHJInfo(int fontSize, String fontName) {
		initDDAZ(fontSize, fontName);
		initDDGZ(fontSize, fontName);
		initSDAZ(fontSize, fontName);
		initSDGZ(fontSize, fontName);
	}
}
